package modules.lobby.view.roomPlayerInfoUI
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import br.com.stimuli.loading.loadingtypes.ImageItem;
	
	import modules.GameDispatcher;
	import modules.load.Load;
	import modules.lobby.Lobby;
	import modules.lobby.LobbyEvent;

	/**
	 * @author lee
	 */
	public class RoomPlayerInfo
	{
		private var mainUI:MovieClip;
		
		private var srcArr:Array = [];
		
		private var playerBtnUPState:Boolean = false; //从小到大排序
		
		private var chipsBtnUPState:Boolean = false;
		
		private var rankBtnUPState:Boolean = false;
		
		private var upArrow:BitmapData;
		private var downArrow:BitmapData;
		private var playerBmp:Bitmap = new Bitmap();
		private var chipBmp:Bitmap = new Bitmap();
		private var rankBmp:Bitmap = new Bitmap();
		
		public function RoomPlayerInfo(mainUI:MovieClip)
		{
			upArrow = Load.getInstance("fla.ui.roomPlayerInfo.upArrow");
			downArrow = Load.getInstance("fla.ui.roomPlayerInfo.downArrow");
			playerBmp.bitmapData = chipBmp.bitmapData = rankBmp.bitmapData = upArrow;
			playerBmp.x=100;
			playerBmp.y=22;
			mainUI.addChild(playerBmp);
			chipBmp.x=224;
			chipBmp.y=22;
			mainUI.addChild(chipBmp);
			rankBmp.x=315;
			rankBmp.y=22;
			mainUI.addChild(rankBmp);
			this.mainUI = mainUI;
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			mainUI.Player.addEventListener(MouseEvent.CLICK,sortOnType);
			mainUI.Chips.addEventListener(MouseEvent.CLICK,sortOnType);
			mainUI.Rank.addEventListener(MouseEvent.CLICK,sortOnType);
			GameDispatcher.instance.addEventListener(LobbyEvent.SHOW_PLAYER_INFO_LIST,setPlayerInfoList);
		}
		
		/**
		 * 显示当前卓玩家信息
		 * @param srcArray 原数据,内容为object  userName,chips,rank
		 */
		public function setPlayerInfoList(event:LobbyEvent):void
		{
			srcArr = event.data;
			mainUI.myRoomInfoBox.removeChildren();
				
			for (var i:int = 0; i < srcArr.length; i++)
			{
				var roomItem:RoomPlayerInfoItem = new RoomPlayerInfoItem();
				roomItem.setDataInfo(srcArr[i].userName, srcArr[i].chips, srcArr[i].rank);
				roomItem.main.y = i*roomItem.main.height;
				mainUI.myRoomInfoBox.addChild(roomItem.main);
			}
		}
		
		private function sortOnType(e:Event):void
		{
			var btnName:String = e.target.text;
			switch (btnName)
			{
				case "Player":
					if (playerBtnUPState == false)
					{
						srcArr.sortOn("userName");
						playerBmp.bitmapData = downArrow;
					}
					else
					{
						srcArr.sortOn("userName", Array.DESCENDING);
						playerBmp.bitmapData = upArrow;
					}
					playerBtnUPState = !playerBtnUPState;
					break;
				case "Chips":
					if (chipsBtnUPState == false)
					{
						srcArr.sortOn("chips");
						chipBmp.bitmapData = downArrow;
					}
					else
					{
						srcArr.sortOn("chips", Array.DESCENDING);
						chipBmp.bitmapData = upArrow;
					}
					chipsBtnUPState = !chipsBtnUPState;
					break;
				case "Rank":
					if (rankBtnUPState == false)
					{
						srcArr.sortOn("rank");
						rankBmp.bitmapData = downArrow;
					}
					else
					{
						srcArr.sortOn("rank",Array.DESCENDING);
						rankBmp.bitmapData = upArrow;
					}
					rankBtnUPState = !rankBtnUPState;
					break;
			}
			setPlayerInfoList(new LobbyEvent("",srcArr));
		}
	}
}